package com.singfan.common.util;

import com.singfan.common.SingFanException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * XML工具类
 * Author: HanSongHe
 * Date: 2015-05-28 10:28
 * Desc: 基于Dom4J封装
 */
public class XmlUtil {
    /**
     * 获取跟节点
     * @param inputStream xml输入流
     * @return 根节点
     */
    public static Element getRoot(InputStream inputStream) {
        if(inputStream != null){
            try {
                SAXReader reader = new SAXReader();
                Document document = reader.read(inputStream);
                return document.getRootElement();
            }
            catch (DocumentException e) {
                throw new SingFanException(String.format("XmlUtil.getRoot() error: %s !", e.getMessage()), e);
            }
        }
        else{
            throw new SingFanException("XmlUtil.getRoot() error: inputStream is null !");
        }
    }

    /**
     * 获取跟节点
     * @param xmlFile xml文件
     * @return 根节点
     */
    public static Element getRoot(File xmlFile){
        if(xmlFile != null && xmlFile.exists()){
            try {
                SAXReader reader = new SAXReader();
                Document document = reader.read(xmlFile);
                return document.getRootElement();
            }
            catch (DocumentException e) {
                throw new SingFanException(String.format("XmlUtil.getRoot() error: %s !", e.getMessage()), e);
            }
        }
        else{
            if (xmlFile == null){
                throw new SingFanException("XmlUtil.getRoot() error: xmlFile is null !");
            }
            else{
                throw new SingFanException(String.format("XmlUtil.getRoot() error: %s is not exist !", xmlFile.getName()));
            }
        }
    }

    /**
     * 获取节点信息
     * @param element 节点
     * @param name 节点名称
     * @param mustExist 是否必须存在
     * @return 节点信息
     */
    public static Element getElement(Element element, String name, boolean mustExist) {
        if (element != null){
            if (!StringUtil.isNullOrEmpty(name)){
                Element childElement = element.element(name);

                if(childElement == null && mustExist){
                    throw new SingFanException(String.format("XmlUtil.getElement() error: node(%1$s) is not exist in node(%2$s) !", name, element.getName()));
                }

                return childElement;
            }
            else {
                throw new SingFanException("XmlUtil.getElement() error: name is null or empty !");
            }
        }
        else {
            throw new SingFanException("XmlUtil.getElement() error: element is null !");
        }
    }

    /**
     * 获取节点属性信息
     * @param element 节点
     * @param name 节点名称
     * @param mustExist 是否必须有值
     * @return 节点属性信息
     */
    public static String getAttributeValue(Element element, String name, boolean mustExist){
        if (element != null){
            if (!StringUtil.isNullOrEmpty(name)){
                String attributeValue = element.attributeValue(name);
                if (mustExist) {
                    if(!StringUtil.isNullOrEmpty(attributeValue) && !StringUtil.isBlank(attributeValue)){
                        return attributeValue;
                    }
                    else{
                        throw new SingFanException(String.format("XmlUtil.getAttributeValue() error: node(%1$s) attribute(%2$s) value is empty !", element.getName(), name));
                    }
                }
                else {
                    return attributeValue;
                }
            }
            else {
                throw new SingFanException("XmlUtil.getAttributeValue() error: name is null or empty !");
            }
        }
        else {
            throw new SingFanException("XmlUtil.getAttributeValue() error: element is null !");
        }
    }

    /**
     * 获取节点属性信息
     * @param element 节点
     * @param mustExist 是否必须有值
     * @return 节点属性信息
     */
    public static String getElementValue(Element element, boolean mustExist){
        if (element != null){
            String value = element.getStringValue();
            if (mustExist) {
                if(!StringUtil.isNullOrEmpty(value) && !StringUtil.isBlank(value)){
                    return value;
                }
                else{
                    throw new SingFanException(String.format("XmlUtil.getElementValue() error: node(%1$s) value is empty !", element.getName()));
                }
            }
            else {
                return value;
            }
        }
        else {
            throw new SingFanException("XmlUtil.getElementValue() error: element is null !");
        }
    }

    /**
     * 获取节点列表信息
     * @param element 节点
     * @param name 节点名称
     * @param mustExist 是否必须存在
     * @return 节点列表信息
     */
    public static List<Element> getElementList(Element element, String name, boolean mustExist) {
        if (element != null){
            if (!StringUtil.isNullOrEmpty(name)){
                List<Element> childElementList = new ArrayList<>();
                List elementList = element.elements(name);

                if (elementList != null && elementList.size() > 0){
                    for(Object obj : elementList) {
                        childElementList.add((Element) obj);
                    }
                }
                else {
                    if(mustExist){
                        throw new SingFanException(String.format("XmlUtil.getElementList() error: node(%1$s) is not exist in node(%2$s) !", name, element.getName()));
                    }
                }
                return childElementList;
            }
            else {
                throw new SingFanException("XmlUtil.getElementList() error: name is null or empty !");
            }
        }
        else {
            throw new SingFanException("XmlUtil.getElementList() error: element is null !");
        }

    }
}
